package Or

import (
	"context"
	"fmt"
	"db2s/global"
	"db2s/go-log/log"
	"db2s/outPut"
	"db2s/parDef"
	"db2s/ref"
	"reflect"
	"strings"
)

// NestedTable 查询嵌套表
func (tm TableMe) NestedTable(ctx context.Context) (result map[string]int, err error) {
	var event = "[NestedTable]"
	var res global.Return
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var schemaTable = getTableName(parDef.GetExecFuncParameterValueBase(ctx))
	if res, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, nestedTableSysObjectView, getTableNameSql, QueryColValManage)); err != nil {
		log.ErrorLog().Error(event, getTableName(parDef.GetExecFuncParameterValueBase(ctx)), fmt.Sprintf(" sql is %v, error is %v.", res.Sql, err))
		return
	}
	result = make(map[string]int)
	switch res.Result.(type) {
	case []string:
		result = ref.StringGarthToMap(res.Result.([]string))
	default:
		log.MainLog().Debug(event, schemaTable, fmt.Sprintf(" sql is %v, bath timeOut is %v ms,result type not matched. query result type is %v.", res.Sql, res.TimeOut, reflect.TypeOf(res.Result)))
	}
	log.MainLog().Debug(event, schemaTable, fmt.Sprintf(" sql is %v, bath timeOut is %v ms,query result is %v.", res.Sql, res.TimeOut, result))
	return
}
func (tm TableMe) NestedTableMarge(ctx context.Context, table any) (newResult []string, err error) {
	var event = "[NestedTableMarge]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var nestedTable map[string]int
	if nestedTable, err = tm.NestedTable(ctx); err != nil {
		err = ref.ErrAddPrintf(event, err)
		return
	}
	switch table.(type) {
	case []string:
		for _, v := range table.([]string) {
			if _, ok := nestedTable[v]; ok {
				continue
			}
			newResult = append(newResult, v)
		}
	}
	return
}
func (tm TableMe) Name(ctx context.Context) (result global.Return, err error) {
	var event = "[Name]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, tableSysObjectView, getTableNameSql, QueryColValManage))
		result.Result, err = tm.NestedTableMarge(ctx, result.Result)
	}
	return
}
func (tm TableMe) Existence(ctx context.Context) (result global.Return, err error) {
	var event = "[Existence]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, tableSysObjectView, getTableExistenceSql, QueryColValManage)); err != nil {
			err = ref.ErrAddPrintf(event, err)
			return
		}
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
func (tm TableMe) Schema(ctx context.Context) (result global.Return, err error) {
	var event = "[Schema]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, userSysObjectView, getSchemaNameSql, QueryColValManage)); err != nil {
			return
		}
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
func (tm TableMe) Comment(ctx context.Context) (result global.Return, err error) {
	var event = "[Comment]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, tableCommentSysObjectView, getTableCommentSql, QueryColValManage)); err != nil {
			return
		}
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
func (tm TableMe) Character(ctx context.Context) (result global.Return, err error) {
	var event = "[Character]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, tableCharacterSysObjectView, getTableCharacterSql, QueryColValManage)); err != nil {
			return
		}
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
func (tm TableMe) Collation(ctx context.Context) (result global.Return, err error) {
	var event = "[Collation]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, tableCharacterSysObjectView, getTableCollationSql, QueryColValManage)); err != nil {
			return
		}
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
func (tm TableMe) PartNameToShardName(ctx context.Context) (result global.Return, err error) {
	var event = "[PartNameToShardName]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	result.Result = table
	return
}
func (tm TableMe) ignoreSubConstraints(result global.Return) (res []parDef.Constraints, err error) {
	var event = "[ignoreSubConstraints]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	if result.Result == nil {
		return
	}
	for _, v := range result.Result.([]map[string]interface{}) {
		vv := mapNullActive(v)
		var re parDef.Constraints
		if err = ref.MapToStruct(vv, &re); err != nil {
			err = ref.ErrAddPrintf("Oracle.Constraints", err)
			return
		}
		if strings.HasSuffix(strings.TrimSpace(re.Body), "IS NOT NULL") {
			continue
		}
		res = append(res, re)
	}
	return
}
func (tm TableMe) Constraints(ctx context.Context) (result global.Return, err error) {
	var event = "[Constraints]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, tableConstraintsSysObjectView, getTableConstraintsSql, QueryColValManage)); err != nil {
			return
		}
		var subConstraints []parDef.Constraints
		subConstraints, err = tm.ignoreSubConstraints(result)
		result.Result = subConstraints
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
func (tm TableMe) Drop(ctx context.Context) (result global.Return, err error) {
	var event = "[Drop]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var schema, table = parDef.GetExecFuncParameterValueBase(ctx)
	result.Sql = dropTableSql(schema, table)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		result.TimeOut, err = newExecUpdateSql(event, getTableName(schema, table), parDef.GetReadExecSqlFuncOptionsValueBase(ctx), []string{result.Sql})
		return
	}
}

func (tm TableMe) Rename(ctx context.Context) (result global.Return, err error) {
	var event = "[Rename]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var schema, table = parDef.GetExecFuncParameterValueBase(ctx)
	result.Sql = renameTableSql(schema, table, parDef.GetParameterValueAnyToString(parDef.GetRecoverSchemaParameterValue()), parDef.GetParameterValueAnyToString(parDef.GetRecoverTableParameterValue()))
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		result.TimeOut, err = newExecUpdateSql(event, getTableName(schema, table), parDef.GetReadExecSqlFuncOptionsValueBase(ctx), []string{result.Sql})
		return
	}
}
func (tm TableMe) Recover(ctx context.Context) (result global.Return, err error) {
	var event = "[Recover]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var schema, table = parDef.GetExecFuncParameterValueBase(ctx)
	var recoverSchema, recoverTable = parDef.GetParameterValueAnyToString(parDef.GetRecoverSchemaParameterValue()), parDef.GetParameterValueAnyToString(parDef.GetRecoverTableParameterValue())
	var execSql = []string{createDatabaseSql(recoverSchema), likeTableSql(recoverSchema, recoverTable, schema, table), renameTableSql(schema, table, recoverSchema, recoverTable), renameTableSql(recoverSchema, recoverTable, schema, table)}
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		result.TimeOut, err = newExecUpdateSql(event, getTableName(schema, table), parDef.GetReadExecSqlFuncOptionsValueBase(ctx), execSql)
		return
	}
}
func (tm TableMe) Truncate(ctx context.Context) (result global.Return, err error) {
	var event = "[Truncate]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var schema, table = parDef.GetExecFuncParameterValueBase(ctx)
	var truncateObject = parDef.GetFuncParameterValue(ctx, parDef.GetTruncateObjectParameterValue())
	var execSql []string
	for _, partName := range truncateObject.([]string) {
		switch partName {
		case "single":
			execSql = append(execSql, truncateTableSql(schema, table))
		default:
			execSql = append(execSql, truncatePartSql(schema, table, partName))
		}
	}
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		result.TimeOut, err = newExecUpdateSql(event, getTableName(schema, table), parDef.GetReadExecSqlFuncOptionsValueBase(ctx), execSql)
		return
	}
}

func (tm TableMe) Analyze(ctx context.Context) (result global.Return, err error) {
	var event = "[Analyze]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var schema, table = parDef.GetExecFuncParameterValueBase(ctx)
	var execSql = []string{analyzeTableColumnSql(schema, table), analyzeTableSql(schema, table)}
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		result.TimeOut, err = newExecUpdateSql(event, getTableName(schema, table), parDef.GetReadExecSqlFuncOptionsValueBase(ctx), execSql)
		return
	}
}

// filePoint outPut.FileOperations
func (tm TableMe) writeFileDDL(event string, ctx context.Context) (result global.Return, err error) {
	event = fmt.Sprintf(" %v -> writeFileDDL", event)
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var schemaTable = getTableName(parDef.GetExecFuncParameterValueBase(ctx))
	var DDLSql = parDef.GetParameterValueAnyToString(parDef.GetFuncParameterValue(ctx, parDef.GetDDLSqlParameterValue()))
	var DDLSqlFilePoint = parDef.GetFuncParameterValue(ctx, parDef.GetDDLSqlFilePointParameterValue())
	log.MainLog().Debug(event, schemaTable, fmt.Sprintf(" sql is %v.", result.Sql))
	if DDLSqlFilePoint == nil {
		err = ref.ErrAddPrintf(event, fmt.Errorf("file point is nil"))
		log.ErrorLog().Error(event, schemaTable, fmt.Sprintf(" sql is %v, error is %v.", result.Sql, err))
		return
	}
	if err = DDLSqlFilePoint.(outPut.FileOperations).Write("", DDLSql); err != nil {
		err = ref.ErrAddPrintf(event, err)
		log.ErrorLog().Error(event, schemaTable, fmt.Sprintf(" sql is %v,file name is %v error is %v.", result.Sql, DDLSqlFilePoint.(outPut.FileOperations).Print(outPut.FileName), err))
		return
	}
	log.MainLog().Debug(event, schemaTable, fmt.Sprintf(" sql is %v, file name is %v bath timeOut is %v ms.", result.Sql, DDLSqlFilePoint.(outPut.FileOperations).Print(outPut.FileName), result.TimeOut))
	return
}
func (tm TableMe) connExecDDL(event string, ctx context.Context) (result global.Return, err error) {
	event = fmt.Sprintf("%v -> connExecDDL", event)
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var DDLSql = parDef.GetParameterValueAnyToString(parDef.GetFuncParameterValue(ctx, parDef.GetDDLSqlParameterValue()))
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		result.TimeOut, err = newExecUpdateSql(event, getTableName(parDef.GetExecFuncParameterValueBase(ctx)), parDef.GetReadExecSqlFuncOptionsValueBase(ctx), []string{DDLSql})
		return
	}
}
func (tm TableMe) ExecDDL(ctx context.Context) (result global.Return, err error) {
	var event = "ExecDDL"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var repairMethod = parDef.GetParameterValueAnyToString(parDef.GetFuncParameterValue(ctx, parDef.GetRepairMethodParameterValue()))
	log.MainLog().Debug(event, getTableName(parDef.GetExecFuncParameterValueBase(ctx)), "start exec ddl sql. exec Method is ", repairMethod)
	select {
	case <-ctx.Done():
		err = ref.ErrAddPrintf(event, fmt.Errorf("context canceled"))
		log.ErrorLog().Error(event, getTableName(parDef.GetExecFuncParameterValueBase(ctx)), "context canceled")
		return
	default:
		switch repairMethod {
		case "table":
			return tm.connExecDDL(event, ctx)
		case "file":
			return tm.writeFileDDL(event, ctx)
		default:
			err = ref.ErrAddPrintf(event, fmt.Errorf(fmt.Sprintf("expect value not matched. curry value is %v expect value is %v", repairMethod, []string{"table", "file"})))
			log.ErrorLog().Error(event, getTableName(parDef.GetExecFuncParameterValueBase(ctx)), fmt.Sprintf(" error is %v.", err))
		}
		log.MainLog().Debug(event, getTableName(parDef.GetExecFuncParameterValueBase(ctx)), "The ddl sql execution has been completed.")
		return
	}
}
func (tm TableMe) CreateGeneral(ctx context.Context) (result global.Return, err error) {
	return
}

func generalPartRulesSql(prefix, meth string, data parDef.PartRule) (res string) {
	var partName string
	switch prefix {
	case "PARTITION":
		partName = data.Name
	case "SUBPARTITION":
		partName = data.SubName
	}
	switch strings.ToUpper(meth) {
	case "RANGE":
		res = fmt.Sprintf("%v \"%v\" VALUES LESS THAN (%v)", prefix, partName, data.Rules)
	case "LIST":
		res = fmt.Sprintf("%v \"%v\" VALUES (%v)", prefix, partName, data.Rules)
	}
	return
}
func generalIndexSql(schema, table string, v parDef.IndexColumnMe) (res string) {
	var indexCol []string
	for _, vv := range v.ColumnMate {
		indexCol = append(indexCol, fmt.Sprintf("%v", vv.ColumnName))
	}
	switch v.ColumnKey {
	case "P":
		res = fmt.Sprintf("ALTER TABLE \"%v\".\"%v\" ADD PRIMARY KEY (\"%v\");", schema, table, strings.Join(indexCol, "\",\""))
	case "U":
		res = fmt.Sprintf("CREATE UNIQUE INDEX \"%v\" ON \"%v\".\"%v\"(\"%v\");", v.IndexName, schema, table, strings.Join(indexCol, "\",\""))
	case "I":
		res = fmt.Sprintf("CREATE INDEX \"%v\" ON \"%v\".\"%v\"(\"%v\");", v.IndexName, schema, table, strings.Join(indexCol, "\",\""))
	}
	return
}
func printCreateIndexSql(schema, table string, metaBase MetaBaseInitResult) (creTabIndex []string) {
	for _, v := range metaBase.indexMeta {
		creTabIndex = append(creTabIndex, generalIndexSql(schema, table, v))
	}
	return
}
func printCreatePartSql(metaBase MetaBaseInitResult) (createPartBody string) {
	var crePartRule []string
	var partRules = partRulesData(metaBase)
	var subPartRules = subPartRulesData(metaBase)
	if !strings.EqualFold(partMethData(metaBase).First, "NULL") && !strings.EqualFold(partMethData(metaBase).First, "NONE") && len(partMethData(metaBase).First) > 0 {
		createPartBody = fmt.Sprintf("PARTITION BY %v (\"%v\")", partMethData(metaBase).First, strings.Join(partColData(metaBase), "\",\""))
	}
	if !strings.EqualFold(partMethData(metaBase).Sub, "NULL") && !strings.EqualFold(partMethData(metaBase).Sub, "NONE") && len(partMethData(metaBase).Sub) > 0 {
		switch partMethData(metaBase).Sub {
		case "HASH", "KEY":
			createPartBody = fmt.Sprintf("%v SUBPARTITION BY %v (\"%v\") \n SUBPARTITIONS %v \n", createPartBody, partMethData(metaBase).Sub, strings.Join(subPartColData(metaBase), "`,`"), len(subPartRules)/len(partRules))
		default:
			createPartBody = fmt.Sprintf("%v\nSUBPARTITION BY %v (\"%v\")", createPartBody, partMethData(metaBase).Sub, strings.Join(subPartColData(metaBase), "\",\""))
		}
	}
	switch partMethData(metaBase).First {
	case "HASH", "KEY":
		crePartRule = append(crePartRule, fmt.Sprintf("PARTITIONS %v ", len(partRules)))
		createPartBody = fmt.Sprintf("%v \n %v\n", createPartBody, strings.Join(crePartRule, ",\n"))
		return
	default:
		for _, v := range partRules {
			var subPartSql []string
			for _, vv := range subPartRules {
				if strings.EqualFold(v.Name, vv.Name) {
					pq := generalPartRulesSql("SUBPARTITION", partMethData(metaBase).Sub, vv)
					if len(pq) > 0 {
						subPartSql = append(subPartSql)
					}
				}
			}
			crePartRule = append(crePartRule, fmt.Sprintf("%v %v", generalPartRulesSql("PARTITION", partMethData(metaBase).First, v), func() string {
				if len(subPartSql) > 0 {
					return fmt.Sprintf("(\n%v\n)", strings.Join(subPartSql, ",\n"))
				}
				return ""
			}()))
		}
	}
	if len(createPartBody) > 0 && len(crePartRule) > 0 {
		createPartBody = fmt.Sprintf("%v (\n%v\n)", createPartBody, strings.Join(crePartRule, ",\n"))
	}
	return
}
func printCreateSingleColumnSql(v parDef.ColumnMeta) (res string) {
	var null, dataType, newColumnDefault string
	switch v.IsNull {
	case "Y":
		null = "NULL"
	case "N":
		null = "NOT NULL"
	}
	dataType = fmt.Sprintf("%v", v.DataType)
	switch {
	case v.CharLength > 0:
		if v.DataLength > 0 {
			dataType = fmt.Sprintf("%v(%v)", v.DataType, v.DataLength)
		}
	default:
		switch strings.ToUpper(v.ColumnType) {
		case "NUMBER":
			if v.DataPrecision > 0 {
				dataType = fmt.Sprintf("%v(%v,%v)", v.DataType, v.DataPrecision, v.DataScale)
			}
		case "FLOAT", "ROWID", "UROWID", "DATE", "TIMESTAMP", "TIMESTAMP WITH TIME ZONE", "TIMESTAMP WITH LOCAL TIME ZONE", "CLOB", "NCLOB", "LONG", "BLOB", "LONG RAW", "RAW":
			if v.DataPrecision > 0 {
				dataType = fmt.Sprintf("%v(%v)", v.DataType, v.DataPrecision)
			}
		}
	}
	switch v.ColumnDefault {
	case "NULL":
	default:
		null = ""
		newColumnDefault = fmt.Sprintf("DEFAULT %v", v.ColumnDefault)
	}
	return fmt.Sprintf("\"%v\" %v %v %v ", v.ColumnName, dataType, null, newColumnDefault)
}
func printCreateColumnSql(schema, table string, metaBase MetaBaseInitResult) (creTabCol []string) {
	var createTableColumn []string
	for _, v := range metaBase.columnMeta {
		createTableColumn = append(createTableColumn, printCreateSingleColumnSql(v))
	}
	creTabCol = append(creTabCol, fmt.Sprintf("\nCREATE TABLE \"%v\".\"%v\" (\n%v\n) \n%v;", schema, table, strings.Join(createTableColumn, ",\n"), printCreatePartSql(metaBase)))
	return
}
func printIndexBody(metaBase MetaBaseInitResult) (res []string) {
	for _, v := range metaBase.indexMeta {
		res = append(res, ref.AnyToJsonString(v))
	}
	return
}
func partMethData(metaBase MetaBaseInitResult) (meth parDef.PartMeth) {
	return metaBase.partMe.Meth
}
func partColData(metaBase MetaBaseInitResult) (partColumn []string) {
	return metaBase.partMe.PartCol
}
func subPartColData(metaBase MetaBaseInitResult) (partColumn []string) {
	return metaBase.partMe.SubPartCol
}
func partRulesData(metaBase MetaBaseInitResult) (partRules []parDef.PartRule) {
	return metaBase.partMe.RuleDesc
}
func subPartRulesData(metaBase MetaBaseInitResult) (partRules []parDef.PartRule) {
	return metaBase.partMe.SubPartDesc
}
func printPartBody(metaBase MetaBaseInitResult) (res []string) {
	res = append(res, fmt.Sprintf("part meth:%v,sub part meth:%v", partMethData(metaBase).First, partMethData(metaBase).Sub))
	res = append(res, fmt.Sprintf("part column:%v,sub part column:%v", partColData(metaBase), subPartColData(metaBase)))
	for _, v := range partRulesData(metaBase) {
		res = append(res, fmt.Sprintf("part rules:%v", ref.AnyToJsonString(v)))
	}
	for _, v := range subPartRulesData(metaBase) {
		res = append(res, fmt.Sprintf("sub part rules:%v", ref.AnyToJsonString(v)))
	}
	return
}
func printColumnBody(metaBase MetaBaseInitResult) (res []string) {
	for _, v := range metaBase.columnMeta {
		res = append(res, ref.AnyToJsonString(v))
	}
	return
}
func columnCommentCreatSql(schema, table string, metaBase MetaBaseInitResult) (creTabColComment []string) {
	for _, v := range metaBase.columnMeta {
		creTabColComment = append(creTabColComment, fmt.Sprintf("COMMENT ON COLUMN \"%v\".\"%v\".\"%v\" IS '%v';", schema, table, v.ColumnName, v.ColumnComment))
	}
	creTabColComment = append(creTabColComment, fmt.Sprintf("COMMENT ON TABLE \"%v\".\"%v\" IS '%v';", schema, table, metaBase.Comment))
	return
}

func (tm TableMe) AlterGeneral(ctx context.Context) (result global.Return, err error) {
	return
}

func (tm TableMe) CreatePrint(ctx context.Context) (result global.Return, err error) {
	var metaBase MetaBaseInitResult
	var res []string
	var schema, table = parDef.GetExecFuncParameterValueBase(ctx)
	if metaBase, err = metaBaseInit(ctx); err != nil {
		err = ref.ErrAddPrintf("CreatePrint", err)
		return
	}
	res = append(res, printColumnBody(metaBase)...)
	res = append(res, printIndexBody(metaBase)...)
	res = append(res, printPartBody(metaBase)...)
	res = append(res, printCreateColumnSql(schema, table, metaBase)...)
	res = append(res, printCreateIndexSql(schema, table, metaBase)...)
	res = append(res, columnCommentCreatSql(schema, table, metaBase)...)
	result.Result = strings.Join(res, "\n")
	return
}
